module Main exposing (..)

import Browser
import Html exposing (Html, button, div, hr, text)
import Html.Events exposing (onClick)

-- MAIN
main =
  Browser.sandbox
      { init = init
      , update = update
      , view = view
      }

-- MODEL
type alias Character =
    { name : String
    , abilities : Abilities
    }
type alias Abilities =
    { str : Int 
    , dex : Int 
    , con : Int 
    , int : Int 
    , wis : Int 
    , cha : Int 
    }
type Ability
    = Strength
    | Dexterity
    | Constitution
    | Intelligence
    | Wisdom
    | Charisma

type alias Model =
    { name : String
    , val : Int
    , character : Character
    }

init : Model
init =
    { name="STR"
    , val=10
    , character =
          { name="Skadi the Brave"
          , abilities =
                { str = 10
                , dex = 10
                , con = 10
                , int = 10
                , wis = 10
                , cha = 10
                }
          }
    }

changeVal : Model -> Int -> Model
changeVal model x =
    { name=model.name
    , val=model.val + x
    , character=model.character
    }

changeAbi : Abilities -> Ability -> Int -> Abilities
changeAbi abilities abi x =
    case abi of
        Strength ->
            { str = abilities.str + x
            , dex = 10
            , con = 10
            , int = 10
            , wis = 10
            , cha = 10
            }
        _ -> abilities

changeAbilities : Model -> Abilities -> Model
changeAbilities model abilities =
    { name=model.name
    , val=model.val
    , character=
          { name = model.character.name
          , abilities = abilities
          }
    }

-- UPDATE
type Msg
  = Increment
  | Decrement
  | IncrementStr
  | DecrementStr

update : Msg -> Model -> Model
update msg model =
  case msg of
    Increment ->
      changeVal model 1
    Decrement ->
      changeVal model -1
    IncrementStr ->
      changeAbilities model (changeAbi model.character.abilities Strength 1)
    DecrementStr ->
      changeAbilities model (changeAbi model.character.abilities Strength -1)

-- VIEW
view : Model -> Html Msg
view model =
  div []
    [ button [ onClick Increment ] [ text "+" ]
    , div [] [ text (model.name ++ ":" ++ String.fromInt model.val) ]
    , button [ onClick Decrement ] [ text "-" ]
    , hr [] []
    , div []
        [ div [] [ text model.character.name ]
        , div []
            [ button [ onClick IncrementStr ] [ text "+" ]
            , div [] [ text ("STR:" ++ String.fromInt model.character.abilities.str) ]
            , button [ onClick DecrementStr ] [ text "-" ]
            ]
        , div [] [ text ("DEX:" ++ String.fromInt model.character.abilities.dex) ]
        , div [] [ text ("CON:" ++ String.fromInt model.character.abilities.con) ]
        , div [] [ text ("INT:" ++ String.fromInt model.character.abilities.int) ]
        , div [] [ text ("WIS:" ++ String.fromInt model.character.abilities.wis) ]
        , div [] [ text ("CHA:" ++ String.fromInt model.character.abilities.cha) ]
        ]
    ]
